package cn.outsourcing.supermarket.provider;

import org.apache.ibatis.jdbc.SQL;

/**
 * 后台用户SQL提供者
 *
 * @author gzkemays
 * @since 2021/11/5 1:05
 */
public class BackstageUserProvider {

  /** 综合数据拼接 */
  public String getComprehensiveData() {
    return new SQL() {
      {
        SELECT("SUM(`number`) as cartSum")
            .SELECT("(" + getOrderSum() + ") as orderSum")
            .SELECT("(" + getOrderDone() + ") as orderDone")
            .SELECT("(" + getOrderMoney() + ") as orderMoney")
            .FROM("`hiolabs_cart`")
            .WHERE("( `user_id` = '${userId}' )")
            .AND()
            .WHERE("( `is_delete` = 0 )");
      }
    }.toString();
  }
  /** 提交订单数 */
  public String getOrderSum() {
    return new SQL() {
      {
        SELECT("COUNT(1)")
            .FROM("`hiolabs_order`")
            .WHERE("( `user_id` = '${userId}' )")
            .AND()
            .WHERE("( `order_type` < 8 )")
            .AND()
            .WHERE("( `is_delete` = 0 )");
      }
    }.toString();
  }

  /** 成交订单 */
  public String getOrderDone() {
    return new SQL() {
      {
        SELECT("COUNT(1)")
            .FROM("`hiolabs_order`")
            .WHERE("( `user_id` = '${userId}' )")
            .AND()
            .WHERE("( `order_type` < 8 )")
            .AND()
            .WHERE("( `order_status` IN ('302','303','401') )")
            .AND()
            .WHERE("( `is_delete` = 0 )");
      }
    }.toString();
  }

  /** 获取订单金额 */
  public String getOrderMoney() {
    return new SQL() {
      {
        SELECT("SUM(`actual_price`)")
            .FROM("`hiolabs_order`")
            .WHERE("( `user_id` = '${userId}' )")
            .AND()
            .WHERE("( `order_type` < 8 )")
            .AND()
            .WHERE("( `order_status` IN ('302','303','401') )")
            .AND()
            .WHERE("( `is_delete` = 0 )");
      }
    }.toString();
  }

  /** 获取购物车数量 */
  public String getCartNum() {
    return new SQL() {
      {
        SELECT("SUM(`number`)")
            .FROM("`hiolabs_cart`")
            .WHERE("( `user_id` = '${userId}' )")
            .AND()
            .WHERE("( `is_delete` = 0 )");
      }
    }.toString();
  }
}
